package com.zr.andealib.util.frequency;

import android.util.Log;

import com.jietong.rfid.uhf.service.R2000ReaderService;
import com.jietong.rfid.uhf.service.impl.R2000ReaderServiceImpl;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.constant.AndeaConstant;

import org.greenrobot.eventbus.EventBus;

/**
 * @author WQJ
 * @create 2019/11/19
 * @desc:高频盘点操作类
 */
public class HighFrequencyCheckUtil {

    private R2000ReaderService readerService = new R2000ReaderServiceImpl();

    public void connectDev() {
        String comm = "/dev/ttyS1";
        int baudRate = 115200;
        if (ReaderUtil.readers == null) {
            ReaderUtil.readers = readerService.serialPortConnect(comm, baudRate, new DataShowInfo());
            if (ReaderUtil.readers != null) {
                String version = readerService.version(ReaderUtil.readers);
                if (version != null) {
                    boolean isSet = readerService.setWorkMode(ReaderUtil.readers, 2);
                }
                if (version == null) {
                    EventBus.getDefault().post(new HardEvent("盘点功能初始化失败，请联系管理员", AndeaConstant.CALL_INIT_CHECK_ERROR));
                }
            } else {
                EventBus.getDefault().post(new HardEvent("盘点功能初始化失败，请联系管理员", AndeaConstant.CALL_INIT_CHECK_ERROR));
            }
        }
    }

    public void disconnect() {
        boolean isTrue = readerService.disconnect(ReaderUtil.readers);
        ReaderUtil.readers = null;
    }

    public void beginInvV2() {
        boolean isTrue = readerService.beginInvV2(ReaderUtil.readers);
        if (!isTrue) {
            EventBus.getDefault().post(new HardEvent("盘点功能开启失败，请联系管理员", AndeaConstant.CALL_INIT_CHECK_ERROR));
            readerService.disconnect(ReaderUtil.readers);
            connectDev();
            readerService.beginInvV2(ReaderUtil.readers);
        }
    }


    public void stopInvV2() {
        if (null == ReaderUtil.readers) {
            EventBus.getDefault().post(new HardEvent("盘点功能未初始化", AndeaConstant.CALL_READ_CHECK_CLOSE_ERROR));
            return;
        }
        boolean isTrue = readerService.stopInvV2(ReaderUtil.readers);
    }
}
